<?php

namespace app\index\controller;

use app\common\model\AchievementAnalysisModel;
use app\common\model\Slideshow;
use app\index\model\Notice;
use app\index\controller\IndexBase;
use app\index\model\Page;

/** 
 *  前端首页控制器
 * 
 * 时间：2020年3月7日23:08:14
 * 作者：夏立军
 *
 */
class Index extends IndexBase
{

    // 首页
    public function index()
    {
        // 通知资讯 
        $notice = new Notice();
        $notice_list = $notice->get_notice_n_data();

        // 轮播图
        $slideshow = new Slideshow();
        $slideshow_info = $slideshow->get_slideshow_info_from_index();

        // 成果分析
        $achievement_analysis = new AchievementAnalysisModel();
        $achievement_analysis_res = $achievement_analysis->get_achievement_analysis_data();

        // 统计总量
        $achievement_analysis_res = json_encode($achievement_analysis_res);


        // 友情链接
        $index_link = config('ams_config.link') ?? [];

        return view('index', [
            'achievement_analysis_res' => $achievement_analysis_res,
            'slideshow_info' => $slideshow_info,
            'notice_list' => $notice_list,
            'index_link' => $index_link,
        ]);
    }

    // 资讯详情
    public function notice()
    {
        $nid = input('get.nid/d', null);
        if (is_null($nid)) {
            return redirect_index('资讯ID丢失', url('Index/index'));
        }

        $notice = new Notice();
        $notice_data = $notice->get_notice_data_by_id($nid);

        if ($notice_data['display'] == 0) {
            return redirect_index('该文章已停用', url('Index/index'));
        }

        $notice_data['image_files'] = json_decode($notice_data['image_files'], true);

        return view('notice', [
            'notice_data' => $notice_data
        ]);
    }

    // 更多资讯
    public function notice_list()
    {
        $notice = new Notice();
        $pageCount = $notice->get_notice_info_count();
        // 获取该页面每页的数量
        $pageSize = config('ams_config.notice_list_size') ?? 20;

        // 调用每页模型
        $page = new Page();
        $page_res = $page->getPageRes($pageCount, $pageSize, url('Index/notice_list'));

        $notice_data = $notice->get_notice_info_page($page_res['offset'], $page_res['size']);

        return view('notice_list', [
            'notice_data' => $notice_data,
            'page_res' => $page_res,
        ]);
    }

    // 友情链接
    public function link()
    {
        // 友情链接
        $index_link = config('ams_config.link') ?? [];
        return view('link', [
            'index_link' => $index_link,
        ]);
    }
}
